﻿//  Copyright (c) 2011 Ray Liang (http://www.dotnetage.com)
//  Licensed MIT: http://www.opensource.org/licenses/mit-license.php

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.Mvc;
using System.Web;

namespace DNA.Mvc.Management
{
    public class Spammers
    {
        public static ISpammerRepository Repository
        {
            get
            {
                return DependencyResolver.Current.GetService<ISpammerRepository>();
            }
        }

        public static void Mark(string userName)
        {
            if (string.IsNullOrEmpty(userName))
                throw new ArgumentNullException("userName");

            if (!Repository.IsSpam(userName))
            {
                var spammer = new Spammer()
                {
                    UserName = userName
                };
                Repository.Create(spammer);
                Repository.Submit();
            }
        }

        public static void Mark(string ip, string url = "")
        {
            if (string.IsNullOrEmpty(ip) && string.IsNullOrEmpty(url))
                throw new ArgumentNullException("ip");

            if (!Repository.IsSpam(ip, url))
            {
                var spammer = new Spammer()
                {
                    IP = ip,
                    Url = url
                };
                Repository.Create(spammer);
                Repository.Submit();
            }
        }

        public static void Mark(HttpContextBase httpContext)
        {
            Mark(httpContext.Request.UserHostAddress, (httpContext.Request.UrlReferrer != null) ? httpContext.Request.UrlReferrer.ToString() : "");
        }

        public static bool IsSpam(string userName)
        {
            return Repository.IsSpam(userName);
        }

        public static bool IsSpam(HttpContextBase httpContext)
        {
            return Repository.IsSpam(httpContext.Request.UserHostAddress, (httpContext.Request.UrlReferrer != null) ? httpContext.Request.UrlReferrer.ToString() : "");
        }

        public static void Unlock(int id)
        {
            var spammer = Repository.Find(id);
            _Delete(spammer);
        }

        private static void _Delete(Spammer spammer)
        {
            if (spammer != null)
            {
                Repository.Delete(spammer);
                Repository.Submit();
            }
        }

        public static void UnlockUser(string userName)
        {
            if (string.IsNullOrEmpty(userName))
                throw new ArgumentNullException("userName");
            _Delete(Repository.Find(s => s.UserName.Equals(userName, StringComparison.OrdinalIgnoreCase)));
        }

        public static void UnlockIP(string ip)
        {
            if (string.IsNullOrEmpty(ip))
                throw new ArgumentNullException("ip");
            _Delete(Repository.Find(s => s.IP.Equals(ip, StringComparison.OrdinalIgnoreCase)));
        }

        public static void UnlockURL(string url)
        {
            if (string.IsNullOrEmpty(url))
                throw new ArgumentNullException("url");
            _Delete(Repository.Find(s => s.Url.Equals(url, StringComparison.OrdinalIgnoreCase)));
        }

    }
}
